package cn.gg.benchmark.test;
import java.util.Vector;

public class Suite implements IExecuable, Runnable {
	private Vector cases = null;
	private Suite container;

	public void setCotainer(Suite cotainer) {
		this.container = cotainer;
	}

	public void setCases(Vector cases) {
		this.cases = cases;
	}

	public void execute() {
		new Thread(this).start();
	}

	public void init() {
		cases = new Vector(5);
		Case aCase = null;
		for (int i = 0; i < 5; i++) {
			aCase = new Case();
			aCase.setContainer(this);
			cases.addElement(aCase);
			aCase = null;
		}
	}

	public synchronized void run() {
		IExecuable exe = null;
		for (int i = 0; i < cases.size(); i++) {
			exe = (IExecuable) cases.elementAt(i);
			System.out.println("-->CASE : " + (cases == null ? 0 : i));
			try {
				exe.init();
				exe.execute();
			} catch (Exception e) {
			} finally {
				exe = null;
				pauseThread();
			}
		}
		release();
	}

	public synchronized void restartThread() {
		this.notify();
	}

	public synchronized void pauseThread() {
		try {
			this.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void release() {
		cases.removeAllElements();
		cases = null;
		if (container != null) {
			container.restartThread();
			container = null;
		}
	}

}
